﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class InOutStockRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15251;
        protected GridView gvInOutStockRpt;
        protected GridView gvInOutStockRptShowLot;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalAJ;
        protected Label lblTotalAJQty;
        protected Label lblTotalBegin;
        protected Label lblTotalBeginQty;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalGrossWGT;
        protected Label lblTotalGrossWGTText;
        protected Label lblTotalIn;
        protected Label lblTotalInQty;
        protected Label lblTotalInv;
        protected Label lblTotalInvQty;
        protected Label lblTotalOt;
        protected Label lblTotalOtQty;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalAJQty;
        private decimal TotalBeginQty;
        private decimal TotalCube;
        private decimal TotalGrossWGT;
        private decimal TotalInQty;
        private decimal TotalInvQty;
        private decimal TotalOtQty;
        protected UpdatePanel up1;

        public void BindData2gvInOutStockList(InOutStockRptQueryEntity InOutStockRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(InOutStockRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetInoutStockRptInfo(InOutStockRptQuery);
            if (InOutStockRptQuery.IsLotAttri == "1")
            {
                base.BindData2GridView(this.gvInOutStockRptShowLot, ds);
            }
            else
            {
                base.BindData2GridView(this.gvInOutStockRpt, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            InOutStockRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = InOutStockRptQuery.CurrentPage;
            InOutStockRptQuery.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetInoutStockRptInfo(InOutStockRptQuery))
                {
                    if (InOutStockRptQuery.IsLotAttri == "1")
                    {
                        base.Export2Excel("InOutStockRpt.xls", "15251", this.gvInOutStockRptShowLot, ds);
                    }
                    else
                    {
                        base.Export2Excel("InOutStockRpt.xls", "15251", this.gvInOutStockRpt, ds);
                    }
                }
            }
            finally
            {
                InOutStockRptQuery.IsGetAll = false;
                InOutStockRptQuery.CurrentPage = currentPageIndex;
                this.SetInOutStockRptQueryEntity(InOutStockRptQuery);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            InOutStockRptQuery.CurrentPage = "0";
            this.BindData2gvInOutStockList(InOutStockRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            InOutStockRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvInOutStockList(InOutStockRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            InOutStockRptQuery.OWNER_ID = "";
            InOutStockRptQuery.SKU_ID = "";
            InOutStockRptQuery.SKUCLASS_ID = "";
            InOutStockRptQuery.STARTDATETIME = "";
            InOutStockRptQuery.ENDDATETIME = "";
            InOutStockRptQuery.DESCR = "";
            InOutStockRptQuery.VENDOR_ID = "";
            InOutStockRptQuery.LOT_ATTR01 = "";
            InOutStockRptQuery.LOT_ATTR02 = "";
            InOutStockRptQuery.LOT_ATTR03 = "";
            InOutStockRptQuery.LOT_ATTR04 = "";
            InOutStockRptQuery.LOT_ATTR05 = "";
            InOutStockRptQuery.LOT_ATTR06 = "";
            InOutStockRptQuery.LOT_ATTR07 = "";
            InOutStockRptQuery.LOT_ATTR08 = "";
            InOutStockRptQuery.LOT_ATTR09 = "";
            InOutStockRptQuery.LOT_ATTR10 = "";
            InOutStockRptQuery.EXPIRY_DATE = "";
            InOutStockRptQuery.PRODUCE_DATE = "";
            InOutStockRptQuery.EXTERNAL_LOT = "";
            this.BindData2gvInOutStockList(InOutStockRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            if (int.Parse(InOutStockRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                InOutStockRptQuery.CurrentPage = (int.Parse(InOutStockRptQuery.CurrentPage) + 1).ToString();
                this.BindData2gvInOutStockList(InOutStockRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            if (int.Parse(InOutStockRptQuery.CurrentPage) > 0)
            {
                InOutStockRptQuery.CurrentPage = (int.Parse(InOutStockRptQuery.CurrentPage) - 1).ToString();
                this.BindData2gvInOutStockList(InOutStockRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
            this.BindData2gvInOutStockList(InOutStockRptQuery);
        }

        private InOutStockRptQueryEntity GetInOutStockRptQueryEntity()
        {
            return (base.PageQueryEntitySession as InOutStockRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
                InOutStockRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvInOutStockList(InOutStockRptQuery);
            }
        }

        protected void gvInOutStockRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15251", this.gvInOutStockRpt);
        }

        protected void gvInOutStockRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblBeginQty")) != null)
                {
                    this.TotalBeginQty += decimal.Parse((((Label) e.Row.FindControl("lblBeginQty")).Text != "") ? ((Label) e.Row.FindControl("lblBeginQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblInQty")) != null)
                {
                    this.TotalInQty += decimal.Parse((((Label) e.Row.FindControl("lblInQty")).Text != "") ? ((Label) e.Row.FindControl("lblInQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblaJQty")) != null)
                {
                    this.TotalAJQty += decimal.Parse((((Label) e.Row.FindControl("lblaJQty")).Text != "") ? ((Label) e.Row.FindControl("lblaJQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblOtQty")) != null)
                {
                    this.TotalOtQty += decimal.Parse((((Label) e.Row.FindControl("lblOtQty")).Text != "") ? ((Label) e.Row.FindControl("lblOtQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblInvQty")) != null)
                {
                    this.TotalInvQty += decimal.Parse((((Label) e.Row.FindControl("lblInvQty")).Text != "") ? ((Label) e.Row.FindControl("lblInvQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblGrossWgt")) != null)
                {
                    this.TotalGrossWGT += decimal.Parse((((Label) e.Row.FindControl("lblGrossWgt")).Text != "") ? ((Label) e.Row.FindControl("lblGrossWgt")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCube")) != null)
                {
                    this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblCube")).Text != "") ? ((Label) e.Row.FindControl("lblCube")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalBeginQty.Text = base.GetPreferenceDigit(this.TotalBeginQty);
                this.lblTotalInQty.Text = base.GetPreferenceDigit(this.TotalInQty);
                this.lblTotalAJQty.Text = base.GetPreferenceDigit(this.TotalAJQty);
                this.lblTotalOtQty.Text = base.GetPreferenceDigit(this.TotalOtQty);
                this.lblTotalInvQty.Text = base.GetPreferenceDigit(this.TotalInvQty);
                this.lblTotalGrossWGTText.Text = base.GetPreferenceDigit(this.TotalGrossWGT);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
            }
        }

        protected void gvInOutStockRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InOutStockRptQueryEntity InOutStockRptQuery = this.GetInOutStockRptQueryEntity();
                InOutStockRptQuery.SortField = e.SortExpression;
                if (InOutStockRptQuery.SortDirection == "ASC")
                {
                    InOutStockRptQuery.SortDirection = "DESC";
                }
                else if (InOutStockRptQuery.SortDirection == "DESC")
                {
                    InOutStockRptQuery.SortDirection = "ASC";
                }
                this.BindData2gvInOutStockList(InOutStockRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvInOutStockRptShowLot_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15251", this.gvInOutStockRptShowLot);
        }

        private void LoadData()
        {
            InOutStockRptQueryEntity InOutStockRptQuery = new InOutStockRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    InOutStockRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    InOutStockRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    InOutStockRptQuery.SKUCLASS_ID = this.PreviousPage.SkuClassID;
                    InOutStockRptQuery.STARTDATETIME = this.PreviousPage.StartTime;
                    InOutStockRptQuery.DESCR = this.PreviousPage.Descr;
                    InOutStockRptQuery.ENDDATETIME = this.PreviousPage.EndTime;
                    InOutStockRptQuery.DESCR = this.PreviousPage.Descr;
                    InOutStockRptQuery.VENDOR_ID = this.PreviousPage.VendorID;
                    InOutStockRptQuery.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    InOutStockRptQuery.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    InOutStockRptQuery.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    InOutStockRptQuery.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    InOutStockRptQuery.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    InOutStockRptQuery.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    InOutStockRptQuery.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    InOutStockRptQuery.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    InOutStockRptQuery.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    InOutStockRptQuery.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    InOutStockRptQuery.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    InOutStockRptQuery.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                    InOutStockRptQuery.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                    InOutStockRptQuery.IsLotAttri = this.PreviousPage.IsLotAttri;
                }
                else
                {
                    InOutStockRptQuery.OWNER_ID = "";
                    InOutStockRptQuery.SKU_ID = "";
                    InOutStockRptQuery.SKUCLASS_ID = "";
                    InOutStockRptQuery.STARTDATETIME = "";
                    InOutStockRptQuery.DESCR = "";
                    InOutStockRptQuery.VENDOR_ID = "";
                    InOutStockRptQuery.LOT_ATTR01 = "";
                    InOutStockRptQuery.LOT_ATTR02 = "";
                    InOutStockRptQuery.LOT_ATTR03 = "";
                    InOutStockRptQuery.LOT_ATTR04 = "";
                    InOutStockRptQuery.LOT_ATTR05 = "";
                    InOutStockRptQuery.LOT_ATTR06 = "";
                    InOutStockRptQuery.LOT_ATTR07 = "";
                    InOutStockRptQuery.LOT_ATTR08 = "";
                    InOutStockRptQuery.LOT_ATTR09 = "";
                    InOutStockRptQuery.LOT_ATTR10 = "";
                    InOutStockRptQuery.EXPIRY_DATE = "";
                    InOutStockRptQuery.PRODUCE_DATE = "";
                    InOutStockRptQuery.EXTERNAL_LOT = "";
                    InOutStockRptQuery.IsLotAttri = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (InOutStockRptQuery.IsLotAttri == "1")
            {
                this.gvInOutStockRptShowLot.Visible = true;
                this.gvInOutStockRpt.Visible = false;
                InOutStockRptQuery.PageSize = this.gvInOutStockRptShowLot.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvInOutStockRpt.PageSize;
                InOutStockRptQuery.SortField = "OWNER_ID,SKU_ID,LOT";
                InOutStockRptQuery.SortDirection = "ASC";
            }
            else
            {
                InOutStockRptQuery.PageSize = this.gvInOutStockRpt.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvInOutStockRpt.PageSize;
                InOutStockRptQuery.SortField = "OWNER_ID,SKU_ID";
                InOutStockRptQuery.SortDirection = "ASC";
            }
            InOutStockRptQuery.IsGetAll = false;
            InOutStockRptQuery.CurrentPage = "0";
            this.SetInOutStockRptQueryEntity(InOutStockRptQuery);
            this.BindData2gvInOutStockList(InOutStockRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInOutStockRpt);
                base.InitAnalyticsReportGridview(this.gvInOutStockRptShowLot);
                this.LoadData();
            }
        }

        public void SetInOutStockRptQueryEntity(InOutStockRptQueryEntity InOutStockRptQuery)
        {
            base.PageQueryEntitySession = InOutStockRptQuery;
        }

        public InOutStockRptQuery PreviousPage
        {
            get
            {
                return (InOutStockRptQuery) base.PreviousPage;
            }
        }
    }
}

